package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;

import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin/level")

public class AdminLevelController {

    @Autowired
    LevelService levelService;


    @SaAdminCheckPermission("admin:level:add")
    @PostMapping("")
    public Result addLevel(@RequestBody LevelCommand levelCommand){
        levelService.addLevel(levelCommand);
        // {"code":200,"message":"成功","data":null} -> 在service中做了异常判断，直接ok即可
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:level:update")
    @PutMapping("")
    public Result updateLevel(@RequestBody LevelCommand levelCommand){
        levelService.updateLevel(levelCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:level:search")
    @GetMapping("list")
    public Result<PageDTO<LevelDTO>> getLevelList(PageRequest pageRequest){     // 使用引用类型接收请求参数
        PageDTO<LevelDTO> levelList = levelService.getLevelList(pageRequest);
        return Result.ok(levelList);
    }

    @SaAdminCheckPermission("admin:leveldelete")
    @DeleteMapping("{id}")
    public Result deleteLevel(@PathVariable("id") Long id){       // 1945398569246953473，需要Long接收
        // 返回值类型是DeleteLevelOperateDTO，因为删除等级这个操作需要记录日志，而影响行数不足以记录日志，需要手动添加更多的信息
        DeleteLevelOperateDTO deleteLevelOperateDTO = levelService.deleteLevel(id);
        // 在service中判断是否删除成功，如果影响行数为0，说明删除失败，抛出自定义异常，异常处理器会捕获
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:level:batchdelete")
    @DeleteMapping("batch")
    public Result deleteLevels(@RequestBody BatchDeleteCommand batchDeleteCommand){
        List<DeleteLevelOperateDTO> operateDTOS = levelService.deleteLevels(batchDeleteCommand);
        return Result.ok();
    }
}
